package com.an.arrays;

import java.util.Arrays;
import java.util.Comparator;

public class HomeWork_1 {
    public static void main(String[] args) {
        Book[] book=new Book[5];
        book[0]=new Book("三国演义",108);
        book[1]=new Book("西游记2834",28);
        book[2]=new Book("红楼梦2",98);
        book[3]=new Book("水浒传25243523225",38);
        book[4]=new Book("java从0到放弃",998);

        //打印
        System.out.println("排序前");
        for (int i = 0; i < book.length ; i++) {
            System.out.println(book[i].toString());
        }
        System.out.println("==============");



        //Book.sort_1(book);
//        Book.sort_2(book, new Compare_2() {
//            @Override
//            public double compare(Book x1, Book x2) {
//                return x1.getPrice()-x2.getPrice();
//            }
//        });
        Arrays.sort(book, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return (int) (o1.getPrice()-o2.getPrice());
            }
        });

        System.out.println("按价格从低到高排序后");
        for (int i = 0; i < book.length ; i++) {
            System.out.println(book[i].toString());
        }


        //降序
        Book.sort_2(book, new Compare_2() {
            @Override
            public double compare(Book x1, Book x2) {
                return x2.getPrice()-x1.getPrice();
            }
        });
        System.out.println("按价格从高到低排序后");
        for (int i = 0; i < book.length ; i++) {
            System.out.println(book[i].toString());
        }
        System.out.println("==============");
        //书名长度
        Book.sort_2(book, new Compare_2() {
            @Override
            public double compare(Book x1, Book x2) {
                return x1.getName().length()-x2.getName().length();
            }
        });
        System.out.println("按书名长度从低到高排序后");
        for (int i = 0; i < book.length ; i++) {
            System.out.println(book[i].toString());
        }
        System.out.println("==============");



    }
}

class Book{
    private String name;
    private double price;
    //排序
    public static void sort_1(Book[] book){
        for(int i=0;i<book.length-1;i++){
            for(int j=0;j<book.length-1-i;j++){
                if(book[j].getPrice()>book[j+1].getPrice()){
                    Book tem=book[j];
                    book[j]=book[j+1];
                    book[j+1]=tem;
                }
            }
        }
    };
    //接口+冒泡
    public static void sort_2(Book[] book,Compare_2 c){
        for(int i=0;i<book.length-1;i++){
            for(int j=0;j<book.length-1-i;j++){
                if(c.compare(book[j],book[j+1])>0){
                    Book tem=book[j];
                    book[j]=book[j+1];
                    book[j+1]=tem;
                }
            }
        }
    };
    //
//    public static void sort_3(Book[] book,Compare_2 c){
//        for(int i=0;i<book.length-1;i++){
//            for(int j=0;j<book.length-1-i;j++){
//                if(c.compare(book[j],book[j+1])>0){
//                    Book tem=book[j];
//                    book[j]=book[j+1];
//                    book[j+1]=tem;
//                }
//            }
//        }
//    };

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

interface Compare_2{
    public double compare(Book x1,Book x2);
}